package com.sugar.psych.assessment.module.business.service.psychquestionbank;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.sugar.psych.assessment.module.business.controller.admin.psychquestionbank.vo.*;
import com.sugar.psych.assessment.module.business.dal.dataobject.psychquestionbank.PsychQuestionBankDO;
import com.sugar.psych.assessment.framework.common.pojo.PageResult;
import com.sugar.psych.assessment.framework.common.pojo.PageParam;
import com.sugar.psych.assessment.framework.common.util.object.BeanUtils;

import com.sugar.psych.assessment.module.business.dal.mysql.psychquestionbank.PsychQuestionBankMapper;

import static com.sugar.psych.assessment.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.sugar.psych.assessment.framework.common.util.collection.CollectionUtils.convertList;
import static com.sugar.psych.assessment.framework.common.util.collection.CollectionUtils.diffList;
import static com.sugar.psych.assessment.module.business.enums.ErrorCodeConstants.*;

/**
 * 心理测评题库 Service 实现类
 *
 * @author Ysugar
 */
@Service
@Validated
public class PsychQuestionBankServiceImpl implements PsychQuestionBankService {

    @Resource
    private PsychQuestionBankMapper psychQuestionBankMapper;

    @Override
    public Long createPsychQuestionBank(PsychQuestionBankSaveReqVO createReqVO) {
        // 插入
        PsychQuestionBankDO psychQuestionBank = BeanUtils.toBean(createReqVO, PsychQuestionBankDO.class);
        psychQuestionBankMapper.insert(psychQuestionBank);
        // 返回
        return psychQuestionBank.getId();
    }

    @Override
    public void updatePsychQuestionBank(PsychQuestionBankSaveReqVO updateReqVO) {
        // 校验存在
        validatePsychQuestionBankExists(updateReqVO.getId());
        // 更新
        PsychQuestionBankDO updateObj = BeanUtils.toBean(updateReqVO, PsychQuestionBankDO.class);
        psychQuestionBankMapper.updateById(updateObj);
    }

    @Override
    public void deletePsychQuestionBank(Long id) {
        // 校验存在
        validatePsychQuestionBankExists(id);
        // 删除
        psychQuestionBankMapper.deleteById(id);
    }

    @Override
        public void deletePsychQuestionBankListByIds(List<Long> ids) {
        // 校验存在
        validatePsychQuestionBankExists(ids);
        // 删除
        psychQuestionBankMapper.deleteByIds(ids);
        }

    private void validatePsychQuestionBankExists(List<Long> ids) {
        List<PsychQuestionBankDO> list = psychQuestionBankMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(PSYCH_QUESTION_BANK_NOT_EXISTS);
        }
    }

    private void validatePsychQuestionBankExists(Long id) {
        if (psychQuestionBankMapper.selectById(id) == null) {
            throw exception(PSYCH_QUESTION_BANK_NOT_EXISTS);
        }
    }

    @Override
    public PsychQuestionBankDO getPsychQuestionBank(Long id) {
        return psychQuestionBankMapper.selectById(id);
    }

    @Override
    public PageResult<PsychQuestionBankDO> getPsychQuestionBankPage(PsychQuestionBankPageReqVO pageReqVO) {
        return psychQuestionBankMapper.selectPage(pageReqVO);
    }

    @Override
    public List<PsychQuestionBankRespVO> getPsychQuestionBankAll() {
        return BeanUtils.toBean(psychQuestionBankMapper.selectList(), PsychQuestionBankRespVO.class);
    }

}